package com.aplink.generic.xmlparser.ctrlformat;

import org.w3c.dom.Node;

import android.text.TextUtils;

import com.aplink.generic.constant.type.EditTextType;
import com.aplink.generic.pojo.controlspec.EditTextSpec;
import com.aplink.generic.xmlparser.XMLPaserUtil;

public class EditTextFormat {
    public static final String INPUTTYPE = "inputtype";
    public static final String ISREADONLY = "readonly";
    public static final String MAXLENGTH = "maxlength";
    public static final String MINLENGTH = "minlength";

    public static EditTextSpec parse(final Node node) {
        final EditTextSpec editTextSpec = new EditTextSpec();
        if (node.hasAttributes()) {
            String value = XMLPaserUtil.getAttrValue(node,
                    EditTextFormat.ISREADONLY);
            if (!TextUtils.isEmpty(value)) {
                if (value.equalsIgnoreCase("Y")) {
                    editTextSpec.setReadOnly(true);
                }
            }

            value = XMLPaserUtil.getAttrValue(node, EditTextFormat.MINLENGTH);
            if (!TextUtils.isEmpty(value)) {
                editTextSpec.setMinLength(Integer.parseInt(value));
            } else {
                editTextSpec.setMinLength(EditTextSpec.MIN_LENGTH);
            }

            value = XMLPaserUtil.getAttrValue(node, EditTextFormat.MAXLENGTH);
            if (!TextUtils.isEmpty(value)) {
                editTextSpec.setMaxLength(Integer.parseInt(value));
            } else {
                editTextSpec.setMaxLength(EditTextSpec.MAX_LENGTH);
            }

            value = XMLPaserUtil.getAttrValue(node, EditTextFormat.INPUTTYPE);
            if (!TextUtils.isEmpty(value)) {
                editTextSpec.setDataInputType(EditTextType.get(value));
            }
        }
        return editTextSpec;
    }
}
